import { get, fetchData } from "../../util/request";
export default {
  state: {
    banners: [],
    searchWord: "",
    songList: [],
    saucerList: [],
    newSongsList:[]
  },
  mutations: {
    getInitData(state, { banners, searchWord, songList, saucerList,newSongsList }) {
      state.banners = banners;
      state.searchWord = searchWord;
      state.songList = songList;
      state.saucerList = saucerList;
      state.newSongsList = newSongsList
    }
  },
  actions: {
    async getInitData({ commit }) {
      // let res = await get('/banner?type=2')
      let res = await fetchData({
        urls: [
          "/banner?type=2", //获取banner
          "/search/default", //获取搜索关键字
          "/personalized?limit=6", //每日推荐
          "/album/newest", //最新专辑
          "/personalized/newsong", //新歌
        ]
      });
      let banners = [];
      let searchWord = "";
      let songList = [];
      let saucerList = [];
      let newSongsList = [];
      if (res[0].code === 200) {
        banners = res[0].banners.map(ban => {
          return {
            imageUrl: ban.pic || ban.imageUrl,
            targetId: ban.targetId,
            targetType: ban.targetType,
            titleColor: ban.titleColor,
            typeTitle: ban.typeTitle,
            url: ban.url,
            video: ban.video,
            song: ban.song
          };
        });
      }

      //搜素关键字
      if (res[1].code === 200) {
        searchWord = res[1].data.showKeyword;
      }

      //每日推荐歌单
      if (res[2].code === 200) {
        songList = res[2].result.map(item => {
          return {
            id: item.id,
            name: item.name,
            pic: item.picUrl,
            playCount: item.playCount,
            trackCount: item.trackCount,
            highQuality: item.highQuality,
            alg: item.alg,
            type:'songLst',
            linkUrl:"",
          };
        });
      }

      //新碟
      if (res[3].code === 200) {
        saucerList = res[3].albums.slice(0,3).map(item => {
          return {
            id: item.id,
            name: item.name,
            pic: item.picUrl,
            type:'albums',
            linkUrl:"",
          };
        });
      }

      //新歌
      if(res[4].code === 200){
        newSongsList = res[4].result.slice(0,3).map(item=>{
          return {
            id:item.id,
            name:item.name,
            pic:item.picUrl,
            linkUrl:"",
            type:'song'
          }
        })
      }

      commit("getInitData", { banners, searchWord, songList, saucerList,newSongsList });
    }
  },
  getters: {}
};
